package com.thundersoft.mm.phone.bt;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.thundersoft.mm.phone.ConnectionManager;
import com.thundersoft.mm.phone.Wifi.WifiP2pSocket;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.UUID;

/**
 * Created by wuchuanlong on 18-2-1.
 */

public abstract class AbstractBluetoothListener {

    protected  String serviceRecordName;
    public static String TAG;
    private StreamThread clientAcceptThread;
    public static boolean connected;
    public AbstractBluetoothListener(){
        serviceRecordName = getClass().getSimpleName()+System.currentTimeMillis();
        TAG = "arlauncher_file_"+getClass().getSimpleName();
    }

    public abstract UUID getUUID();
    public abstract void processBodyBuffer(byte[] buffer, int count);
    public abstract void processHeadBuffer(byte[] buffer, int count);

    public void startListener(){
        release();
        clientAcceptThread = null;
        clientAcceptThread = new StreamThread();
        clientAcceptThread.start();
    }

    protected  InputStream inputStream;
    protected OutputStream outputStream;
    protected ByteBuffer outputLenBuffer = ByteBuffer.allocate(HEAD_BUFFER_SIZE);
    protected BluetoothServerSocket serverSocket;
    protected BluetoothSocket bluetoothSocket;
    protected static final int HEAD_BUFFER_SIZE = 16;
    protected static final int MAX_HEAD_LENGTH = 1024 * 1024;
    protected static final int MAX_BODY_LENGTH = 1024 * 1024 * 20;

    public synchronized void sendMsg(String message){
        try {
            byte[] bodyByte = message.getBytes("utf-8");
            sendMsg(bodyByte);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public abstract void sendMsg(byte[] bodyByte);

    public abstract void processStream();

    protected final void listener(){
        try {
            serverSocket = BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord(
                    serviceRecordName, getUUID());
            Log.i(TAG,"==============listener================="+getUUID()+"   "+serviceRecordName);
            bluetoothSocket = serverSocket.accept();

            Log.i(TAG,"==============bluetoothSocket accept================="+getClass().getSimpleName());
            inputStream = bluetoothSocket.getInputStream();
            outputStream = bluetoothSocket.getOutputStream();
            WifiP2pSocket.listener();
//            ConnectionManager.wifiConnection.init(ConnectionManager.mContext);
            connected = true;
        }catch (Exception e){
            Log.i(TAG,"listener error");
            e.printStackTrace();
        }
    }

    private class StreamThread extends Thread {
        public void run() {
            processStream();
        }
    }
    protected boolean positionRelease = false;
    public void release(){
        release(false);
    }

    public void release(boolean position){
        connected = false;
        positionRelease = position;
        if (inputStream != null) {
            try {
                inputStream.close();
            }catch (Exception e){

            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            }catch (Exception e){

            }
        }
        if (serverSocket != null) {
            try {
                serverSocket.close();
            }catch (Exception e){

            }
        }
        if (bluetoothSocket != null) {
            try {
                bluetoothSocket.close();
            }catch (Exception e){

            }
        }
        serverSocket = null;
        bluetoothSocket = null;
        inputStream = null;
        outputStream = null;
//        ConnectionManager.wifiConnection.release();
//        ConnectionManager.mGlassesIp = null;
        WifiP2pSocket.release();
    }
}
